package com.sczfdf.www.sda.gov.cn.dao;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import java.util.Vector;

import org.apache.log4j.Logger;

/**
 * 懒汉式数据库连接池
 * @author Gb
 *
 */
public class MySqlConnectionPool {
	static Logger log = Logger.getLogger(MySqlConnectionPool.class);
	
	private Vector<Connection> pool;
	private String url;
	private String username;
	private String password;
	private String driverClassName;
	private int poolSize = 5;
	private static volatile MySqlConnectionPool instance = new MySqlConnectionPool();

	//私有构造方法，禁止外部创建本类的对象，要想获得本类的对象，通过<code>getInstance</code>方法
	private MySqlConnectionPool(){
		init();
		log.info("数据库链接创建完毕,个数:"+pool.size());
		System.out.println("数据库链接创建完毕,个数:"+pool.size());
	}
	
	public int getPoolSize(){
		return pool.size();
	}

	//连接池初始化方法，读取属性文件的内容，建立连接池中的初始连接
	private void init(){
		readConfig();
		pool = new Vector<Connection>(poolSize);
		addConnection(poolSize);
	}

	//返回连接到连接池中
	public synchronized void release(Connection coon){
		pool.add(coon);
	}

	//关闭连接池中的所有数据库连接
	public synchronized void closePool(){
		for (int i = 0; i < pool.size(); i++) {
			try {
				((Connection)pool.get(i)).close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
			pool.remove(i);
		}
	}

	//返回当前连接池的一个对象
	public static MySqlConnectionPool getInstance(){
//		if (instance == null) {
//			synchronized (MySqlConnectionPool.class) {
//                if (instance == null) {
//                    instance = new MySqlConnectionPool();
//                }
//            }
//		}
		return instance;
	}

	//返回连接池中的一个数据库连接
	public synchronized Connection getConnection(){
		if (pool.size() <= 0) {
			addConnection(1);
		}
		Connection conn = pool.get(0);
		pool.remove(conn);
		return conn;
	}

	//在连接池中创建初始设置的数据库连接
	private void addConnection(int max){
		Connection coon = null;
		for (int i = 0; i < max; i++) {
			try {
				Class.forName(driverClassName);
				coon = java.sql.DriverManager.getConnection(url, username, password);
				pool.add(coon);
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	//读取设置连接池的属性文件
	private void readConfig(){
		try {
			String path = "jdbc.properties";
			FileInputStream is = new FileInputStream(path);
			Properties props = new Properties();
			props.load(is);
			this.driverClassName = props.getProperty("driverClassName");
			this.username = props.getProperty("username");
			this.password = props.getProperty("password");
			this.url = props.getProperty("url");
			this.poolSize = Integer.parseInt(props.getProperty("maxActive"));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.error("mysql:属性文件找不到");
		} catch (IOException e) {
			e.printStackTrace();
			log.error("mysql:读取属性文件出错");
		}
	}

//	public static void main(String[] args) throws SQLException {
//		
//         for (int i = 0; i < 2; i++) {
//             pool = ConnectionPool.getInstance();
//             Connection conn = pool.getConnection();
//             PreparedStatement stmt = conn.prepareStatement(sql);
//             ResultSet rs = stmt.executeQuery(sql);
//             while (rs.next()) {
//                 System.out.println(rs.getString(1) + "\t\t" + rs.getString(2) + "\t\t" + rs.getString(3));
//             }
//             rs.close();
//             stmt.close();
//             pool.release(conn);
//         }
//         pool.closePool();
//	}
}